var utils = require("../utils/utils");
var defaultActionConfig = {
  interval: null, //每个多少秒执行一次
  canStartHour: null, //最早可以开始的时间
  canStartHourEnd: null, //最晚可以开始的时间
  lastStartTime: null, //上一次的开始时间
  lastEndTime: null, //上一次的结束时间
  execSCount: 0, //执行成功次数
  execFCount: 0, //执行失败次数
  maxExecSCount: null, //最大成功执行次数
  maxExecFCount: null, //最大失败执行次数
  maxTotalExecCount: null, //最大总执行次数
  storage: null,
};
function ACTION(name, config) {
  this.name = name;
  utils.merge(this, config || {}, defaultActionConfig);
}
ACTION.prototype.getStorage = function () {
  try {
    return JSON.parse(this.storage.get(this.name));
  } catch (e) {
    return {
      lastStartTime: null,
      lastEndTime: null,
      execSCount: 0,
      execFCount: 0,
    };
  }
};
ACTION.prototype.updateStorage = function () {
  this.storage.put(
    this.name,
    JSON.stringify({
      lastStartTime: this.lastStartTime,
      lastEndTime: this.lastEndTime,
      execSCount: this.execSCount,
      execFCount: this.execFCount,
    })
  );
};
ACTION.prototype.goStart = function () {
  return true;
};
ACTION.prototype.beforeExec = function () {
  return true;
};
ACTION.prototype.exec = function () {};
ACTION.prototype.afterExec = function () {};
ACTION.prototype.beforeEnd = function () {};
ACTION.prototype.end = function () {};
ACTION.prototype.afterEnd = function () {};
ACTION.prototype.canFlow = function () {
  utils.merge(this, this.getStorage() || {}, {
    lastStartTime: null,
    lastEndTime: null,
    execSCount: 0,
    execFCount: 0,
  });
  if (this.canStartHour) {
    var hour = new Date().getHours();
    if (hour < this.canStartHour) {
      console.log(this.name, "最早开始时间未到", hour, "/", this.canStartHour);
      return false;
    }
  }
  if (this.canStartHourEnd) {
    var hour = new Date().getHours();
    if (hour >= this.canStartHourEnd) {
      console.log(
        this.name,
        "最晚结束时间已过",
        hour,
        "/",
        this.canStartHourEnd
      );
      return false;
    }
  }
  if (this.interval && this.lastEndTime) {
    var now = Date.now();
    var hasRun = (now - this.lastEndTime) / 1000;
    if (hasRun < interval) {
      console.log(this.name, "间隔时间未到", hasRun, this.interval);
      return false;
    }
  }
  if (this.maxTotalExecCount) {
    if (this.execSCount == this.maxExecSCount) {
      console.log(this.name, "执行成功次数已达上限");
      return false;
    }
    if (this.execSCount + this.execFCount < this.maxTotalExecCount) {
      return true;
    } else {
      console.log(this.name, "总执行次数已达上限");
      return false;
    }
  }
  if (this.maxExecFCount) {
    if (this.execSCount == this.maxExecSCount) {
      console.log(this.name, "执行成功次数已达上限");
      return false;
    }
    if (this.execFCount < this.maxExecFCount) {
      return true;
    } else {
      console.log(this.name, "执行失败次数已达上限");
      return false;
    }
  }
  if (this.maxExecSCount) {
    if (this.execSCount < this.maxExecSCount) {
      return true;
    } else {
      console.log(this.name, "执行成功次数已达上限");
      return false;
    }
  }
  return true;
};
ACTION.prototype.flow = function () {
  console.show(true);
  if (!this.canFlow()) {
    sleep(utils.getRandom(1.5, 2, 1000));
    console.hide();
    return false;
  }
  if (this.goStart()) {
    var beforeExec = this.beforeExec();
    if (beforeExec) {
      this.exec(beforeExec);
      this.afterExec();
      this.beforeEnd();
      this.end();
      this.afterEnd();
    }
    sleep(utils.getRandom(1.5, 2, 1000));
    console.hide();
  } else {
    console.log(this.name, "到达开始页面失败");
    sleep(utils.getRandom(1.5, 2, 1000));
    console.hide();
  }
};

module.exports = ACTION;
